<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" prop="id" label="类目id">
      </el-table-column>
      <el-table-column prop="pid" label="父类id">
      </el-table-column>
      <el-table-column prop="name" label="电影类名">
        <template slot-scope="scope">
        <span v-show="!scope.row.show">{{scope.row.name}}</span>
        <el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.name"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="alias_name" label="类别名">
        <template slot-scope="scope">
          <span v-show="!scope.row.show">{{scope.row.alias_name}}</span>
          <el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.alias_name"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <span v-show="scope.row.status == 1">正常</span>
          <span v-show="scope.row.status == 0">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="source_id" label="来源id">
      </el-table-column>
      <el-table-column prop="create_time" label="更新时间">
      </el-table-column>
      <el-table-column prop="update_time" label="更新时间">
      </el-table-column>
      <el-table-column label="操作"
                       width="250px">
        <template slot-scope="scope">
          <el-button v-show="!scope.row.show" @click="editRow(scope.row)">编辑</el-button>
          <el-button v-show="scope.row.show" type="success" @click="rowSave(scope.row)">保存</el-button>
          <el-button type="danger" @click="rowDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getMovieCategoryList,updateMovieCategory,deleteMovieCategory } from '@/api/movieCategory'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        1:'正常',
        0:'禁用'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getMovieCategoryList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    editRow(row){
      row.show = !row.show
    },
    rowSave(row){
      let params = {
        id:row.id,
        pid:row.pid,
        name:row.name,
        alias_name:row.alias_name
      }
      updateMovieCategory(params).then(response => {
        if(response.data == 1){
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          });
        }
      })
      row.show = !row.show
      this.fetchData()
    },
    rowDelete(row){
      let params = {
        id:row.id
      }
      this.$confirm('确认删除该条记录', '确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success',
          callback: action => {
            if (action === 'confirm') {
              deleteMovieCategory(params).then(response => {
                if(response.data == 1){
                  this.$message({
                    showClose: true,
                    message: '删除成功',
                    type: 'success'
                  });
                }
              })
              this.fetchData()
            }
            else {
              return
            }
          }
        })
    }
  }
}
</script>
